package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.pojo.Goods;
import com.pinyougou.service.GoodsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Reference
    private GoodsService goodsService;


    @GetMapping("/findByPage")
    public PageResult findByPage(Goods goods, Integer page , @RequestParam(defaultValue = "10")Integer rows){

        try{
            /** 添加查询条件 */
            goods.setAuditStatus("0");
            /** GET请求中文转码 */
            if (StringUtils.isNoneBlank(goods.getGoodsName())){
                goods.setGoodsName(new String(
                        goods.getGoodsName().getBytes("ISO8859-1"),"UTF-8"));
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        /** 调用服务层分页查询 */
        return goodsService.findByPage(goods, page, rows);
    }
    @GetMapping("/updateStatus")
    public boolean updateStatus(Long[] ids,String status){
        try {
            goodsService.updateStatus("audit_status",ids,status);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    @GetMapping("/delete")
    public boolean delete(Long[] ids){
        try {
            goodsService.updateStatus("is_delete",ids,"1");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    /** 商家商品上下架(修改可销售状态) */
    @GetMapping("/updateMarketable")
    public boolean updateMarketable(Long[] ids, String status){
        try{
            // 修改数据库表可销售状态
            goodsService.updateStatus("is_marketable", ids, status);

            return true;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return false;
    }



}























